<template>
  <div>
    <el-card class="box-card">
      <el-row>
        <span id="title">用户注册</span>
        <el-divider></el-divider>
      </el-row>
      <el-form :model="user" status-icon :rules="rules" ref="ruleForm" label-width="100px" class="demo-ruleForm container">
        <el-form-item label="用户名" prop="uname">
          <el-input v-model="user.uname"></el-input>
        </el-form-item>
        <el-form-item label="密码" prop="password">
          <el-input type="password" v-model="user.password" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="确认密码" prop="checkPass">
          <el-input type="password" v-model="user.checkPass" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="用户类型" prop="type">
          <el-select v-model="user.type" clearable placeholder="请选择用户类型">
            <el-option label="个人投资用户" value="个人投资用户"></el-option>
            <el-option label="企业投资用户" value="企业投资用户"></el-option>
            <el-option label="个人借款用户" value="个人借款用户"></el-option>
            <el-option label="企业借款用户" value="企业借款用户"></el-option>
            <el-option label="个人担保用户" value="个人担保用户"></el-option>
            <el-option label="企业担保用户" value="企业担保用户"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="用户姓名" prop="name" v-if="user.type.startsWith('个人')">
          <el-input v-model="user.name"></el-input>
        </el-form-item>
        <el-form-item label="用户手机号" prop="phone" v-if="user.type.startsWith('个人')">
          <el-input v-model="user.phone"></el-input>
        </el-form-item>
        <el-form-item label="用户性别" prop="gender" v-if="user.type.startsWith('个人')">
          <el-radio-group v-model="user.gender">
            <el-radio-button label="男"></el-radio-button>
            <el-radio-button label="女"></el-radio-button>
          </el-radio-group>
        </el-form-item>
        <el-form-item label="身份证号" prop="idcard" v-if="user.type.startsWith('个人')">
          <el-input v-model="user.idcard"></el-input>
        </el-form-item>
        <el-form-item label="银行卡号" prop="bankcard" v-if="user.type.startsWith('个人')">
          <el-input v-model="user.bankcard"></el-input>
        </el-form-item>
        <el-form-item label="企业名称" prop="firmname" v-if="user.type.startsWith('企业')">
          <el-input v-model="user.firmname"></el-input>
        </el-form-item>
        <el-form-item label="法人代表" prop="legalperson" v-if="user.type.startsWith('企业')">
          <el-input v-model="user.legalperson"></el-input>
        </el-form-item>
        <el-form-item label="法人手机号" prop="phone" v-if="user.type.startsWith('企业')">
          <el-input v-model="user.phone"></el-input>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="submitForm('ruleForm')">提交</el-button>
          <el-button @click="resetForm('ruleForm')">重置</el-button>
        </el-form-item>
      </el-form>
    </el-card>
  </div>
</template>

<script>
export default {
  data() {
    var validateCheckName = (rule, value, callback) => {
      this.$axios.get('/user/checkUname/' + value).then(res=>{
        if (res.data.code == 200) {
          if (res.data.data) {
            callback();
          } else {
            callback(new Error('您输入的用户名已存在'));
          }
        }
      })
    };
    var validatePass = (rule, value, callback) => {
      if (this.user.checkPass !== '') {
        this.$refs.ruleForm.validateField('checkPass');
      }
      callback();
    };
    var validatePass2 = (rule, value, callback) => {
      if (value !== this.user.password) {
        callback(new Error('两次输入密码不一致!'));
      } else {
        callback();
      }
    };
    return {
      user:{
        uname:'',
        password:'',
        checkPass:'',
        type:'',
        name:'',
        gender:'',
        phone:'13931234567',
        idcard:'410402191912121212',
        bankcard:'5238479324756913485',
        firmname:'',
        legalperson:''
      },
      rules: {
        uname: [
          { required: true, message: '请输入用户名', trigger: 'blur' },
          { validator: validateCheckName, trigger: 'blur' }
        ],
        password: [
          { required: true, message: '请输入密码', trigger: 'blur' },
          { validator: validatePass, trigger: 'blur' }
        ],
        checkPass: [
          { required: true, message: '请再次输入密码', trigger: 'blur' },
          { validator: validatePass2, trigger: 'blur' }
        ],
        type: [
          { required: true, message: '请选择用户类型', trigger: 'change' }
        ],
        name: [
          { required: true, message: '请输入姓名', trigger: 'blur' },
        ],
        gender: [
          { required: true, message: '请选择性别', trigger: 'change' },
        ],
        phone: [
          { required: true, message: '请输入手机号', trigger: 'blur' },
          { 
            pattern: /^(13[0-9]|14[01456879]|15[0-35-9]|16[2567]|17[0-8]|18[0-9]|19[0-35-9])\d{8}$/, 
            message: '请输入正确的手机号',
            trigger: 'blur'
          }
        ],
        idcard: [
          { required: true, message: '请输入身份证号', trigger: 'blur' },
          { 
            pattern: /^[1-9]\d{5}(18|19|20|(3\d))\d{2}((0[1-9])|(1[0-2]))(([0-2][1-9])|10|20|30|31)\d{3}[0-9Xx]$/, 
            message: '请输入正确的身份证号',
            trigger: 'blur'
          }
        ],
        bankcard: [
          { required: true, message: '请输入银行卡号', trigger: 'blur' },
        ],
        firmname: [
          { required: true, message: '请输入企业名称', trigger: 'blur' },
        ],
        legalperson: [
          { required: true, message: '请输入法人代表', trigger: 'blur' },
        ],
      }
    }
  },
  mounted(){
  },
  methods:{
    submitForm(formName) {
      this.$refs[formName].validate((valid) => {
        if (valid) {
          let data = {}
          if (this.user.type.startsWith('个人')) {
            data = {
              uname:this.user.uname,
              password:this.user.password,
              type:this.user.type,
              name:this.user.name,
              legalperson:this.user.name,
              gender:this.user.gender,
              phone:this.user.phone,
              idcard:this.user.idcard,
              bankcard:this.user.bankcard,
            }
          }
          if (this.user.type.startsWith('企业')) {
            data = {
              uname:this.user.uname,
              password:this.user.password,
              name:this.user.legalperson,
              type:this.user.type,
              phone:this.user.phone,
              firmname:this.user.firmname,
              legalperson:this.user.legalperson
            }
          }
          this.$axios.post("/user/register",data).then(res=>{
            if (res.data.code == 200) {
              this.$message({
                type: 'success',
                message: '注册成功!'
              });
              this.user = {
                uname:'',
                password:'',
                checkPass:'',
                type:'',
                name:'',
                gender:'',
                phone:'13931234567',
                idcard:'410402191912121212',
                bankcard:'5238479324756913485',
                firmname:'',
                legalperson:''
              }
            }
          })
        } else {
          console.log('error submit!!');
          return false;
        }
      });
    },
    resetForm(formName) {
      this.$refs[formName].resetFields();
    }
  }
}
</script>

<style scoped>
.box-card {
  margin-left:5vh;
  margin-right:2vh;
  margin-top: 3vh;
  height: 88vh;
  display: flex;          /* 使元素成为 Flex 容器 */
  justify-content: center; /* 水平居中 */
  align-items: center;     /* 垂直居中 */
  overflow-y: auto; /* 使用 "auto" 只在需要时显示滚动条，或用 "scroll" 始终显示滚动条 */
  flex-direction: column; /* 确保内容按列排列，以便可以滚动 */
}
.el-input{
  width: 50vh;
}
#title{
  text-align: center;
  display: block;
  font-size: 6vh;
}
</style>